﻿
// SakerProjectDlg.cpp: 实现文件
//

#include "pch.h"
#include "iipch.h"
#include "framework.h"
#include "SakerProject.h"
#include "SakerProjectDlg.h"
#include "afxdialogex.h"

#include "Project.h"

#include "xxUIdesign.h"
#include "xxFileProcess.h"
#include "xxWidgetControl.h"
#include "xxSystemInfo.h"

#include "mmShowImage.h"
#include "mmProduct.h"
#include "mmConnect_PLC.h"
#include "mmConnect_MesPost.h"

#include "File_Logs_Dlg.h"
#include "File_Login_Dlg.h"
#include "File_Paras_Dlg.h"
#include "File_Notice_Dlg.h"


#include "mmConnect_COM.h"

#include "Project.h"

#include "Connect_PLC_Dlg.h"
#include "Connect_MesPost_Dlg.h"

#include "LTDMC_IO_Dlg.h"
#include "LTDMC_AxisIO_Dlg.h"
#include "LTDMC_AxisControl_Dlg.h"
#include "LTDMC_Pos_Dlg.h"
#include "LTDMC_CalibPos_Dlg.h"

#include "CapImg_Paras_Dlg.h"
#include "P220201A_Lijing_Calib_0_Dlg.h"

#include "Test_BadPoint_Dlg.h"
#include "Test_Stain_Dlg.h"
#include "Test_RollAngle_Dlg.h"

#include "Calib_GetCorners_Dlg.h"
#include "Calib_IntrinPara_Dlg.h"
#include "Calib_Flags_Dlg.h"
#include "Calib_IntrinInit_Dlg.h"
#include "Calib_IntrinResult_Dlg.h"

#include "Tools_SetChart_Dlg.h"

#include "SystemAuto.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSakerProjectDlg 对话框
CSakerProjectDlg* m_SakerProject;


CSakerProjectDlg::CSakerProjectDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SakerProject, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	// 判断程序是否重复开启，如果重复开启了就推出当前程序
	HANDLE hMutex = CreateMutex(NULL, FALSE, L"SakerProject");
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
		MessageBox(L"程序已经运行", L"提示", MB_OK);
		exit(-1);
	}
}

void CSakerProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CSakerProjectDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_File_Logs, &CSakerProjectDlg::OnFileLogs)
	ON_COMMAND(ID_File_Login, &CSakerProjectDlg::OnFileLogin)
	ON_COMMAND(ID_File_Paras, &CSakerProjectDlg::OnFileParas)
	ON_BN_CLICKED(IDC_BUTTON_Test, &CSakerProjectDlg::OnBnClickedButtonTest)
	ON_WM_SIZE()
	ON_COMMAND(ID_CapImg_Paras, &CSakerProjectDlg::OnCapimgParas)
	ON_BN_CLICKED(IDC_BUTTON_Stop, &CSakerProjectDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_Start, &CSakerProjectDlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_Save, &CSakerProjectDlg::OnBnClickedButtonSave)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_CHECK_AutoProcess, &CSakerProjectDlg::OnBnClickedCheckAutoprocess)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_ClearSum, &CSakerProjectDlg::OnBnClickedButtonClearsum)
	ON_BN_CLICKED(IDC_BUTTON_ClearProductNo, &CSakerProjectDlg::OnBnClickedButtonClearproductno)
	ON_BN_CLICKED(IDC_CHECK_StdProduct, &CSakerProjectDlg::OnBnClickedCheckStdproduct)
	ON_COMMAND(ID_File_Notice, &CSakerProjectDlg::OnFileNotice)
	ON_COMMAND(ID_Connect_PLC, &CSakerProjectDlg::OnConnectPlc)
	ON_COMMAND(ID_Calib_GetCorners, &CSakerProjectDlg::OnCalibGetcorners)
	ON_COMMAND(ID_Calib_IntrinPara, &CSakerProjectDlg::OnCalibIntrinpara)
	ON_COMMAND(ID_Tools_SetChart, &CSakerProjectDlg::OnToolsSetchart)
	ON_COMMAND(ID_LTDMC_IO, &CSakerProjectDlg::OnLtdmcIo)
	ON_COMMAND(ID_LTDMC_AxisIO, &CSakerProjectDlg::OnLtdmcAxisio)
	ON_COMMAND(ID_LTDMC_AxisControl, &CSakerProjectDlg::OnLtdmcAxiscontrol)
	ON_COMMAND(ID_Calib_Flags, &CSakerProjectDlg::OnCalibFlags)
	ON_BN_CLICKED(IDC_BUTTON_LTDMCHome, &CSakerProjectDlg::OnBnClickedButtonLtdmchome)
	ON_COMMAND(ID_LTDMC_Pos, &CSakerProjectDlg::OnLtdmcPos)
	ON_COMMAND(ID_Project_General_0, &CSakerProjectDlg::OnProjectGeneral0)
	ON_STN_DBLCLK(IDC_STATIC_Figs, &CSakerProjectDlg::OnStnDblclickStaticFigs)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_Logs, &CSakerProjectDlg::OnNMDblclkListLogs)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_Logs, &CSakerProjectDlg::OnNMCustomdrawListLogs)
	ON_COMMAND(ID_LTDMC_CalibPos, &CSakerProjectDlg::OnLtdmcCalibpos)
	ON_COMMAND(ID_Calib_IntrinResult, &CSakerProjectDlg::OnCalibIntrinResult)
	ON_COMMAND(ID_Calib_IntrinInit, &CSakerProjectDlg::OnCalibIntrininit)
	ON_COMMAND(ID_Test_Stain, &CSakerProjectDlg::OnTestStain)
	ON_COMMAND(ID_Test_BadPoint, &CSakerProjectDlg::OnTestBadpoint)
	ON_BN_CLICKED(IDC_BUTTON_ClearProductNo2, &CSakerProjectDlg::OnBnClickedButtonClearproductno2)
	ON_COMMAND(ID_Test_RollAngle, &CSakerProjectDlg::OnTestRollangle)
	ON_COMMAND(ID_Connect_MesPost, &CSakerProjectDlg::OnConnectMespost)
	ON_BN_CLICKED(IDC_CHECK_NO_SCAN, &CSakerProjectDlg::OnBnClickedCheckNoScan)
	ON_BN_CLICKED(IDC_CHECK_NO_NGBOX, &CSakerProjectDlg::OnBnClickedCheckNoNgbox)
END_MESSAGE_MAP()


// CSakerProjectDlg 消息处理程序

BOOL CSakerProjectDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	

	m_SakerProject = this;

	// 通用初始化
	m_File_Logs.Create(IDD_File_Logs, this); // 初始化日志
	mmFile_Logs.InitMainDlg((CListCtrl*)GetDlgItem(IDC_LIST_Logs));

	mmFile_Login._Init(); // 初始化用户登录
	mmFile_Paras._Init(); // 初始化项目设置

	// 项目选择初始化
	Project._Init();

	mmConnect_COM = new _mmConnect_COM;
	mmConnect_COMPower = new _mmConnect_COMPower;


	mmFile_Notice._Init(); // 初始化操作说明
	// 采图初始化
	gSystemTM._Init();
	gSystemCI._Init();
	// 产品初始化
	InitmmProduct();

	mmConnect_MesPost = new _mmConnect_MesPost;



	// 当前页面初始化

	vecWidgetRects = xx::GetWidgetRects(this);
	xx::CButtonIco(this, IDC_BUTTON_Start, IDI_ICON_Start, 30, 10);
	xx::CButtonIco(this, IDC_BUTTON_Stop, IDI_ICON_Stop, 30, 10);
	xx::CButtonIco(this, IDC_BUTTON_Save, IDI_ICON_Save, 30, 10);
	xx::ResizeWidgetFont(&CurStateFont, 30, GetDlgItem(IDC_STATIC_CurState));
	xx::ResizeWidgetFont(&AutoProcessFont, 40, GetDlgItem(IDC_CHECK_AutoProcess));
	xx::ResizeWidgetFont(&StdProductFont, 30, GetDlgItem(IDC_CHECK_StdProduct));
	xx::ResizeWidgetFont(&ProductResultFont, 100, GetDlgItem(IDC_STATIC_ProductResult));
	SetDlgItemText(IDC_STATIC_ProductResult, L"");
	SetDlgItemText(IDC_STATIC_ProductResult2, L"");
	SetTimer(nTimer, 100, NULL);

	mmShowImage.m_hWnd = GetDlgItem(IDC_STATIC_Figs)->GetSafeHwnd();



	xx::WriteWidget(this, IDC_SHOW_ProjectNo, Project.ProjectNo);
	xx::WriteWidget(this, IDC_SHOW_ModelNo, mmFile_Paras.ModelNo);

#ifdef iiRelease
	GetDlgItem(IDC_BUTTON_Test)->ShowWindow(TRUE);
#endif // iiRelease

	SetWindowText(L"SakerProject V2.0");

	iiProject.Flag_InitSystem = false;
	mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT_YELW].SetTrigStatus(true);

	CurProject.mainWind = this;

	return TRUE;
}

void CSakerProjectDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else {
		xx::SetBackColor(this, iiCOLOR_RGB_SAKER_BK);
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CSakerProjectDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CSakerProjectDlg::DispTestData(uuIntrin* data)
{
	CString str;
	str.Format(L"%.3f", data->u0);
	SetDlgItemText(IDC_STATIC_CX, str);

	str.Format(L"%.3f", data->v0);
	SetDlgItemText(IDC_STATIC_CY, str);

	str.Format(L"%.3f", data->fx);
	SetDlgItemText(IDC_STATIC_U, str);	// 控件ID好像写错了

	str.Format(L"%.3f", data->fy);
	SetDlgItemText(IDC_STATIC_V, str);

	str.Format(L"%.3f", data->k1);
	SetDlgItemText(IDC_STATIC_KK1, str);

	str.Format(L"%.3f", data->k2);
	SetDlgItemText(IDC_STATIC_KK2, str);

	str.Format(L"%.3f", data->k3);
	SetDlgItemText(IDC_STATIC_KK3, str);

	str.Format(L"%.3f", data->k4);
	SetDlgItemText(IDC_STATIC_KK4, str);
}


void CSakerProjectDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED)//窗体大小发生变动。处理程序
	{
		if (vecWidgetRects.size() != 0) {
			xx::ResizeWindow(this, vecWidgetRects);
		}
	}
	Invalidate();//更新窗口
}

static UINT ThreadT(LPVOID pParam) {
	CString cstr;
	for (int i = 0; i < 100; i++) {
		cstr.Format(L"TEST %d", i);
		mmFile_Logs.NewLog(cstr);
		gSystemCI.Flag_RunCapture = TRUE;
		while (TRUE) {
			Sleep(10);
			if (gSystemCI.Flag_CaptureRunning == TRUE) {
				break;
			}
			if (gSystemCI.Flag_RunCapture == false) {
				break;
			}
		}

		gSystemCI.Flag_RunCapture = false;
		while (TRUE) {
			Sleep(10);
			if (gSystemCI.Flag_CaptureRunning == false) {
				break;
			}
			if (gSystemCI.Flag_RunCapture == TRUE) {
				break;
			}
		}
	}
	return 0;
}

void CSakerProjectDlg::OnBnClickedButtonTest()
{
	AfxBeginThread(ThreadT, this);
	return;



	CString cstr;
	cstr.Format(L"%sAutoSaveFig/%s/%s_%02d.bmp", mmFile_Paras.ModelReportFolderPath(), xx::GetSysDate(), mmProduct.ProductNo, 0);
	mmFile_Logs.NewLog(cstr);

	bool cjk = TRUE;
}


void CSakerProjectDlg::OnBnClickedButtonStart()
{
	iiProject.Flag_StopProcess = false;
	gSystemCI.Flag_RunCapture = TRUE;

}

void CSakerProjectDlg::OnBnClickedButtonStop()
{
	gSystemAT.Flag_AutoProcess = false;
	gSystemCI.Flag_RunCapture = false;
	iiProject.Flag_StopProcess = TRUE;
	mmLTDMC.Stop();
}

void CSakerProjectDlg::OnBnClickedButtonSave()
{
	gSystemTM.ManualSaveFig();
}

void CSakerProjectDlg::OnBnClickedCheckAutoprocess()
{
	iiProject.Flag_StopProcess = false;

	if (gSystemAT.Flag_AutoProcess == false) {
		if (mmLTDMC.Flag_AllAxisHomed == false) {
			mmFile_Logs.NewLog(L"尚未回原, 请先回原", ooLogsLV_ERROR);
			return;
		}
		if (mmLTDMC.CheckPos(_CurProject::nnPos_Standby) == false) {
			mmFile_Logs.NewLog(L"不在上料待机位, 请先回原", ooLogsLV_ERROR);
			return;
		}
		mmLTDMC.OutBits[0][_CurProject::nnOutbit_PRDT_OK].SetTrigStatus(TRUE);
		mmLTDMC.OutBits[0][_CurProject::nnOutbit_PRDT_NG].SetTrigStatus(false);
	}
	gSystemAT.Flag_AutoProcess = !gSystemAT.Flag_AutoProcess;
}

void CSakerProjectDlg::OnBnClickedCheckStdproduct()
{
	mmProduct.Flag_StdProduct = !mmProduct.Flag_StdProduct;
}

void CSakerProjectDlg::ShowCurrent(float current)
{
	CString str;
	if (mmConnect_COMPower->Flag_AutoConnect)
		str.Format(L"%3.1f mA", mmConnect_COMPower->current);
	else
		str.Format(L"%3.1f mA", current);

	 
	SetDlgItemText(IDC_STATIC_CURRENT, str);
}

void CSakerProjectDlg::InitmmProduct() {
	mmProduct.MainDlg = this;
	mmProduct.m_CWnd_TotalNum = GetDlgItem(IDC_SHOW_TotalNum);
	mmProduct.m_CWnd_NGNum = GetDlgItem(IDC_SHOW_NGNum);
	mmProduct.m_CWnd_ProductNo = GetDlgItem(IDC_SHOW_ProductNo);
	mmProduct.m_CWnd_ProductResult = GetDlgItem(IDC_STATIC_ProductResult);
	mmProduct.m_CWnd_ProductNo2 = GetDlgItem(IDC_SHOW_ProductNo2);
	mmProduct.m_CWnd_ProductResult2 = GetDlgItem(IDC_STATIC_ProductResult2);
	mmProduct._Init();
}

BOOL CSakerProjectDlg::PreTranslateMessage(MSG* pMsg)
{
	mmProduct.ParseKeyboardBarcode(pMsg);

	if (pMsg->message == WM_KEYDOWN) return TRUE; // 不对消息进行处理

	return CDialogEx::PreTranslateMessage(pMsg);
}

void CSakerProjectDlg::OnNMCustomdrawListLogs(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMLVCUSTOMDRAW* lplvcd = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
	mmFile_Logs.MainDlgCustomDraw(lplvcd, pResult);
}



HBRUSH CSakerProjectDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	if (hbr != nullptr) {
		DeleteObject((HGDIOBJ)hbr);
	}
	switch (pWnd->GetDlgCtrlID())
	{
	case IDC_SHOW_ProjectNo:
		hbr = CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_SHOW_ModelNo:
		hbr = CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_GeneralState:
		hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK); break;
	case IDC_STATIC_SafetyErr:
		hbr = (iiProject.Flag_SafetyErr) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
	case IDC_STATIC_CapImg:
		hbr = (gSystemCI.Flag_CaptureRunning) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_RED); break;
	case IDC_STATIC_PLC:
		if (mmConnect_PLC.Flag_Inited == false) { hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK); break; }
		hbr = (mmConnect_PLC.Flag_Connected) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_RED); break;
	case IDC_STATIC_LTDMCHome:
		if (mmLTDMC.Flag_Inited == false) { hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK); break; }
		hbr = (mmLTDMC.Flag_AllAxisHomed) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_RED); break;
	case IDC_CHECK_AutoProcess:
		hbr = (gSystemAT.Flag_AutoProcess == TRUE) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_SAKER_BK);
		break;
	case IDC_CHECK_StdProduct:
		hbr = (mmProduct.Flag_StdProduct == TRUE) ? CreateSolidBrush(iiCOLOR_RGB_GREEN) : CreateSolidBrush(iiCOLOR_RGB_SAKER_BK);
		break;
	case IDC_CHECK_NO_SCAN:
		hbr = (CurProject.isNoScan) ? CreateSolidBrush(iiCOLOR_RGB_RED) : CreateSolidBrush(iiCOLOR_RGB_GREEN);
		break;
	case IDC_SHOW_ProductNo:
		switch (mmProduct.ProductNoStatus) {
		case ooProductNoStatus_Init: hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK); break;
		case ooProductNoStatus_Wait: hbr = CreateSolidBrush(iiCOLOR_RGB_WHITE); break;
		case ooProductNoStatus_Success: hbr = CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
		case ooProductNoStatus_Error: hbr = CreateSolidBrush(iiCOLOR_RGB_RED); break;
		}
		break;
	case IDC_STATIC_ProductResult:
		if (gSystemAT.Flag_AutoProcess == false) {
			hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK);
		}
		else {
			switch (mmProduct.Result->Value) {
			case ooProductResult_Clear: hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK); break;
			case ooProductResult_OK: hbr = CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
			case ooProductResult_NG: hbr = CreateSolidBrush(iiCOLOR_RGB_RED); break;
			}
		}
		break;
	case IDC_SHOW_ProductNo2:
		switch (mmProduct.ProductNoStatus2) {
		case ooProductNoStatus_Init: hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK); break;
		case ooProductNoStatus_Wait: hbr = CreateSolidBrush(iiCOLOR_RGB_WHITE); break;
		case ooProductNoStatus_Success: hbr = CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
		case ooProductNoStatus_Error: hbr = CreateSolidBrush(iiCOLOR_RGB_RED); break;
		}
		break; break;
	case IDC_STATIC_ProductResult2:
		if (gSystemAT.Flag_AutoProcess == false) {
			hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK);
		}
		else {
			switch (mmProduct.Result2->Value) {
			case ooProductResult_Clear: hbr = CreateSolidBrush(iiCOLOR_RGB_SAKER_BK); break;
			case ooProductResult_OK: hbr = CreateSolidBrush(iiCOLOR_RGB_GREEN); break;
			case ooProductResult_NG: hbr = CreateSolidBrush(iiCOLOR_RGB_RED); break;
			}
		}
		break;
	case IDC_STATIC_CurState:
		switch (gSystemAT.StepLV) {
		case ooLogsLV_DEBUG:
			hbr = CreateSolidBrush(iiFILE_LOGS_COLOR_RGB_DEBUG); break;
		case ooLogsLV_EVENT:
			hbr = CreateSolidBrush(iiCOLOR_RGB_WHITE); break;
		case ooLogsLV_ERROR:
			hbr = CreateSolidBrush(iiFILE_LOGS_COLOR_RGB_ERROR); break;
		case ooLogsLV_WARNING:
			hbr = CreateSolidBrush(iiFILE_LOGS_COLOR_RGB_WARNING); break;
		default:
			hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
			break;
		}
		break;
	default:
		hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
		break;
	}
	pDC->SetBkMode(TRANSPARENT);
	return hbr;
}

void CSakerProjectDlg::OnCancel()
{
	KillTimer(nTimer);
	iiProject.Flag_ExitSystem = TRUE;

	Sleep(100);

	CDialogEx::OnCancel();
}

void CSakerProjectDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == nTimer) {
		xx::RewriteWidget(GetDlgItem(IDC_STATIC_SafetyErr));
		xx::RewriteWidget(GetDlgItem(IDC_STATIC_CapImg));
		xx::RewriteWidget(GetDlgItem(IDC_STATIC_PLC));
		xx::RewriteWidget(GetDlgItem(IDC_STATIC_LTDMCHome));

		((CButton*)GetDlgItem(IDC_BUTTON_Start))->EnableWindow(!gSystemCI.Flag_RunCapture && !gSystemAT.Flag_AutoProcess);
		//((CButton*)GetDlgItem(IDC_BUTTON_Stop))->EnableWindow(gSystemCI.Flag_RunCapture || gSystemAT.Flag_AutoProcess);
		((CButton*)GetDlgItem(IDC_CHECK_AutoProcess))->SetCheck(gSystemAT.Flag_AutoProcess);


		if (mmLTDMC.InBits[0][_CurProject::nnInbit_EM_STOP].GetTrigStatus())
		{
			mmLTDMC.Flag_AllAxisHomed = false;
			gSystemAT.Flag_AutoProcess = false;
			mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT__RED].SetTrigStatus(true);
			mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT_GREN].SetTrigStatus(false);
			mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT_YELW].SetTrigStatus(false);
			mmLTDMC.OutBits[0][_CurProject::nnOutbit_BEERING].SetTrigStatus(true);
			Sleep(500);
			mmLTDMC.OutBits[0][_CurProject::nnOutbit_BEERING].SetTrigStatus(false);
			Sleep(500);
		}
		else
		{
			if (gSystemAT.Flag_AutoProcess)
			{
				mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT__RED].SetTrigStatus(false);
				mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT_GREN].SetTrigStatus(TRUE);
				mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT_YELW].SetTrigStatus(false);
			}
			else
			{
				mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT__RED].SetTrigStatus(false);
				mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT_GREN].SetTrigStatus(false);
				mmLTDMC.OutBits[0][_CurProject::nnOutbit_LT_YELW].SetTrigStatus(true);
			}
		}
	}

	CDialogEx::OnTimer(nIDEvent);
}

void CSakerProjectDlg::OnBnClickedButtonClearsum()
{
	if (AfxMessageBox(L"确定清空统计数据？", MB_OKCANCEL) == IDOK) {
		mmFile_Logs.NewLog(L"AT: 清空统计数据");
		mmProduct.UpdateTotalNum(0);
		mmProduct.UpdateNGNum(0);

	}
}

void CSakerProjectDlg::OnBnClickedButtonClearproductno()
{
	mmProduct.ClearBarcode(1);
}

void CSakerProjectDlg::OnBnClickedButtonClearproductno2()
{
	mmProduct.ClearBarcode(2);
}

void CSakerProjectDlg::OnStnDblclickStaticFigs()
{
	if (mmFile_Logs.Flag_MainDlgLC_Inited == false) {
		return;
	}
	GetDlgItem(IDC_LIST_Logs)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_STATIC_Figs)->ShowWindow(SW_HIDE);
}

void CSakerProjectDlg::OnNMDblclkListLogs(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

	GetDlgItem(IDC_STATIC_Figs)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_LIST_Logs)->ShowWindow(SW_HIDE);

	*pResult = 0;
}

BOOL FLAG_BUTTON_HOME_RUNNING = false;
static UINT thread_button_home(LPVOID pParam) {
	if (AfxMessageBox(L"是否回原？", MB_OKCANCEL) != IDOK) {
		FLAG_BUTTON_HOME_RUNNING = false;
		return false;
	}
	//mmShowChart.ShowCurrent();
	//Sleep(1000);
	mmFile_Logs.NewLog(L"LTDMC: 开始回原", ooLogsLV_DEBUG);
	//mmLTDMC.Move(-2000, 5, 1);

	mmLTDMC.Home();

	double nTimeOut = 0;
	//mmFile_Logs.NewLog(L"LTDMC: 增距镜下落", ooLogsLV_DEBUG);
	//CurProject.Run_RL_Home();
	while (iiProject.Flag_ExitSystem == false) {

		Sleep(100);
		nTimeOut += 0.1;

		if (nTimeOut > 60) {
			mmFile_Logs.NewLog(L"回原超时", ooLogsLV_ERROR);
			return false;
		}

		if (mmLTDMC.Flag_AllAxisHomed == TRUE) {
			mmFile_Logs.NewLog(L"所有轴回原完成");
			break;
		}
		if (iiProject.Flag_StopProcess == TRUE) {
			return false;
		}
	}
	mmLTDMC.Move2Pos(_CurProject::nnPos_Standby);

	nTimeOut = 0;
	while (iiProject.Flag_ExitSystem == false) {

		Sleep(100);
		nTimeOut += 0.1;

		if (nTimeOut > 20) {
			mmFile_Logs.NewLog(L"到达 上料待机位 超时", ooLogsLV_ERROR);
			return false;
		}

		if (mmLTDMC.Flag_AllAxisHomed == TRUE) {
			mmFile_Logs.NewLog(L"到达 上料待机位 完成");
			break;
		}
		if (iiProject.Flag_StopProcess == TRUE) {
			return false;
		}
	}
	mmFile_Logs.NewLog(L"等待开启自动流程");
	FLAG_BUTTON_HOME_RUNNING = false;

	return TRUE;
}

void CSakerProjectDlg::OnBnClickedButtonLtdmchome()
{
	if (FLAG_BUTTON_HOME_RUNNING) {
		mmFile_Logs.NewLog(L"手动回原进行中");
		return;
	}
	iiProject.Flag_StopProcess = false;
	FLAG_BUTTON_HOME_RUNNING = TRUE;
	AfxBeginThread(thread_button_home, this);
}












void CSakerProjectDlg::OnFileLogs()
{
	if (mmFile_Logs.Flag_LC_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	m_File_Logs.ShowWindow(SW_SHOW);
	CRect rect0, rect1, rect;
	m_File_Logs.GetWindowRect(rect0);
	m_SakerProject->GetWindowRect(rect1);
	rect.left = rect1.left;
	rect.top = rect1.top + 80;
	rect.right = rect.left + rect0.Width();
	rect.bottom = rect.top + rect0.Height();
	m_File_Logs.MoveWindow(rect);
}

void CSakerProjectDlg::OnFileLogin()
{
	if (mmFile_Login.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_File_Login != nullptr) {
		delete m_File_Login;
	}
	m_File_Login = new CFile_Login_Dlg;
	m_File_Login->Create(IDD_File_Login, this);
	m_File_Login->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnFileParas()
{
	if (mmFile_Paras.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_File_Paras != nullptr) {
		delete m_File_Paras;
	}
	m_File_Paras = new CFile_Paras_Dlg;
	m_File_Paras->Create(IDD_File_Paras, this);
	m_File_Paras->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnFileNotice()
{
	if (mmFile_Notice.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_File_Notice != nullptr) {
		delete m_File_Notice;
	}
	m_File_Notice = new CFile_Notice_Dlg;
	m_File_Notice->Create(IDD_File_Notice, this);
	m_File_Notice->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnConnectPlc()
{
	//if (mmConnect_COMPower->Flag_Inited == false /*&& mmFile_Login.CurUser.Permission != ooPermission_Admin*/) {
	//	return;
	//}
	if (m_Connect_PLC != nullptr) {
		delete m_Connect_PLC;
	}
	m_Connect_PLC = new CConnect_PLC_Dlg;
	m_Connect_PLC->Create(IDD_Connect_PLC, this);
	m_Connect_PLC->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnConnectMespost()
{
	if (m_Connect_MesPost != nullptr) {
		delete m_Connect_MesPost;
	}
	m_Connect_MesPost = new CConnect_MesPost_Dlg;
	m_Connect_MesPost->Create(IDD_Connect_MesPost, this);
	m_Connect_MesPost->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnLtdmcIo()
{
	if (mmLTDMC.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_LTDMC_IO != nullptr) {
		delete m_LTDMC_IO;
	}
	m_LTDMC_IO = new CLTDMC_IO_Dlg;
	m_LTDMC_IO->Create(IDD_LTDMC_IO, this);
	m_LTDMC_IO->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnLtdmcAxisio()
{
	if (mmLTDMC.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_LTDMC_AxisIO != nullptr) {
		delete m_LTDMC_AxisIO;
	}
	m_LTDMC_AxisIO = new CLTDMC_AxisIO_Dlg;
	m_LTDMC_AxisIO->Create(IDD_LTDMC_AxisIO, this);
	m_LTDMC_AxisIO->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnLtdmcAxiscontrol()
{
	if (mmLTDMC.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_LTDMC_AxisControl != nullptr) {
		delete m_LTDMC_AxisControl;
	}
	m_LTDMC_AxisControl = new CLTDMC_AxisControl_Dlg;
	m_LTDMC_AxisControl->Create(IDD_LTDMC_AxisControl, this);
	m_LTDMC_AxisControl->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnLtdmcPos()
{
	if (mmLTDMC.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_LTDMC_Pos != nullptr) {
		delete m_LTDMC_Pos;
	}
	m_LTDMC_Pos = new CLTDMC_Pos_Dlg;
	m_LTDMC_Pos->Create(IDD_LTDMC_Pos, this);
	m_LTDMC_Pos->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnLtdmcCalibpos()
{
	if ((mmLTDMC.Flag_Inited == false || mmCalib.Flag_Inited == false) && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_LTDMC_CalibPos != nullptr) {
		delete m_LTDMC_CalibPos;
	}
	m_LTDMC_CalibPos = new CLTDMC_CalibPos_Dlg;
	m_LTDMC_CalibPos->Create(IDD_LTDMC_CalibPos, this);
	m_LTDMC_CalibPos->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnCapimgParas()
{
	if (m_CapImg_Paras != nullptr) {
		delete m_CapImg_Paras;
	}
	m_CapImg_Paras = new CCapImg_Paras_Dlg;
	m_CapImg_Paras->Create(IDD_CapImg_Paras, this);
	m_CapImg_Paras->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnProjectGeneral0()
{
	if (m_P220201A_Lijing_Calib_0 != nullptr) {
		delete m_P220201A_Lijing_Calib_0;
	}
	m_P220201A_Lijing_Calib_0 = new CP220201A_Lijing_Calib_0_Dlg;
	m_P220201A_Lijing_Calib_0->Create(IDD_P220201A_Lijing_Calib_0, this);
	m_P220201A_Lijing_Calib_0->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnTestStain()
{
	if (mmTest_Stain.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Test_Stain != nullptr) {
		delete m_Test_Stain;
	}
	m_Test_Stain = new CTest_Stain_Dlg;
	m_Test_Stain->Create(IDD_Test_Stain, this);
	m_Test_Stain->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnTestBadpoint()
{
	if (mmTest_BadPoint.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Test_BadPoint != nullptr) {
		delete m_Test_BadPoint;
	}
	m_Test_BadPoint = new CTest_BadPoint_Dlg;
	m_Test_BadPoint->Create(IDD_Test_BadPoint, this);
	m_Test_BadPoint->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnTestRollangle()
{
	if (mmTest_RollAngle.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Test_RollAngle != nullptr) {
		delete m_Test_RollAngle;
	}
	m_Test_RollAngle = new CTest_RollAngle_Dlg;
	m_Test_RollAngle->Create(IDD_Test_RollAngle, this);
	m_Test_RollAngle->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnCalibGetcorners()
{
	if (mmCalib.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Calib_GetCorners != nullptr) {
		delete m_Calib_GetCorners;
	}
	m_Calib_GetCorners = new CCalib_GetCorners_Dlg;
	m_Calib_GetCorners->Create(IDD_Calib_GetCorners, this);
	m_Calib_GetCorners->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnCalibIntrinpara()
{
	if (mmCalib.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Calib_IntrinPara != nullptr) {
		delete m_Calib_IntrinPara;
	}
	m_Calib_IntrinPara = new CCalib_IntrinPara_Dlg;
	m_Calib_IntrinPara->Create(IDD_Calib_IntrinPara, this);
	m_Calib_IntrinPara->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnCalibFlags()
{
	if (mmCalib.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Calib_Flags != nullptr) {
		delete m_Calib_Flags;
	}
	m_Calib_Flags = new CCalib_Flags_Dlg;
	m_Calib_Flags->Create(IDD_Calib_Flags, this);
	m_Calib_Flags->ShowWindow(SW_SHOW);
}

void CSakerProjectDlg::OnCalibIntrininit()
{
	if (mmCalib.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Calib_IntrinInit != nullptr) {
		delete m_Calib_IntrinInit;
	}
	m_Calib_IntrinInit = new CCalib_IntrinInit_Dlg;
	m_Calib_IntrinInit->Create(IDD_Calib_IntrinInit, this);
	m_Calib_IntrinInit->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnCalibIntrinResult()
{
	if (mmCalib.Flag_Inited == false && mmFile_Login.CurUser.Permission != ooPermission_Admin) {
		return;
	}
	if (m_Calib_IntrinResult != nullptr) {
		delete m_Calib_IntrinResult;
	}
	m_Calib_IntrinResult = new CCalib_IntrinResult_Dlg;
	m_Calib_IntrinResult->Create(IDD_Calib_IntrinResult, this);
	m_Calib_IntrinResult->ShowWindow(SW_SHOW);
}


void CSakerProjectDlg::OnToolsSetchart()
{
	if (m_Tools_SetChart != nullptr) {
		delete m_Tools_SetChart;
	}
	m_Tools_SetChart = new CTools_SetChart_Dlg;
	m_Tools_SetChart->Create(IDD_Tools_SetChart, this);
	m_Tools_SetChart->ShowWindow(SW_SHOW);
}





















void CSakerProjectDlg::OnBnClickedCheckNoScan()
{
	CurProject.isNoScan = !CurProject.isNoScan;
}


void CSakerProjectDlg::OnBnClickedCheckNoNgbox()
{
	// TODO: 在此添加控件通知处理程序代码
	CurProject.isPassNGBox = !CurProject.isPassNGBox;
}
